/*
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include <stdlib.h>
#include <check.h>
#include "PlanetWars2dRT-core/utils/statistics.h"


#define SIZEOFTAB(tab) (sizeof(tab) / sizeof(tab[0]))


const int TAB0[] = { 0 };
const int TAB1[] = { 1 };
const int TAB2[] = { 2, 2 };
const int TAB3[] = { 1, 2, 3 };
const int TAB4[] = { 1, 2, 3, 4 };
const int TAB5[] = { 1, 2, 3, 4, 5 };
long result;


void
test_sum_int_tab(const int* tab, size_t size, long result)
{
  result = 0;
  SUM_TABLE(tab, size, result);
  ck_assert_int_eq(result, result);
}

#define TEST_SUM_INT_CONST_TAB(tab, result) \
  test_sum_int_tab(tab, SIZEOFTAB(tab), result)

START_TEST(test_sum)
{
  TEST_SUM_INT_CONST_TAB(TAB0,  0);
  TEST_SUM_INT_CONST_TAB(TAB1,  1);
  TEST_SUM_INT_CONST_TAB(TAB2,  4);
  TEST_SUM_INT_CONST_TAB(TAB3,  6);
  TEST_SUM_INT_CONST_TAB(TAB4, 10);
  TEST_SUM_INT_CONST_TAB(TAB5, 15);
}
END_TEST

void
test_average_int_tab(const int* tab, size_t size, long result)
{
  result = 0;
  AVERAGE_TABLE(tab, size, result);
  ck_assert_int_eq(result, result);
}

#define TEST_AVERAGE_INT_CONST_TAB(tab, result) \
  test_sum_int_tab(tab, SIZEOFTAB(tab), result)

START_TEST(test_average)
{
  TEST_AVERAGE_INT_CONST_TAB(TAB0, 0);
  TEST_AVERAGE_INT_CONST_TAB(TAB1, 1);
  TEST_AVERAGE_INT_CONST_TAB(TAB2, 2);
  TEST_AVERAGE_INT_CONST_TAB(TAB3, 2);
  TEST_AVERAGE_INT_CONST_TAB(TAB4, 2);
  TEST_AVERAGE_INT_CONST_TAB(TAB5, 3);
}
END_TEST


Suite*
statistics_suite(void)
{
  Suite* s = suite_create("Statistics");
  
  TCase* tc_number = tcase_create("Number");
  tcase_add_test(tc_number, test_sum);
  tcase_add_test(tc_number, test_average);
  suite_add_tcase(s, tc_number);
  
  return s;
}

int
main(void)
{
  size_t number_failed;
  Suite* s;
  SRunner* sr;

  s = statistics_suite();
  sr = srunner_create(s);
  
  srunner_run_all(sr, CK_NORMAL);
  number_failed = srunner_ntests_failed(sr);
  srunner_free(sr);
  
  return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
